<?php
/**
 * Created by PhpStorm.
 * User: sunzexin
 * Date: 2018/3/20
 * Time: 10:33
 */

namespace App\Http\Logics\MiniProgram;


use App\Address;
use App\Http\Logics\BaseLogic;

class AddressLogic extends BaseLogic
{
    public static function getAddressList($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        $query = Address::where(function($query) use ($user_id, $data){
            $query->where('user_id', $user_id);
            if (isset($data['is_default']) && $data['is_default']){
                $query->where('is_default', $data['is_default']);
            }
        })->orderBy('id', 'desc');

        if (isset($data['is_default']) && $data['is_default']){
            $address = $query->first();
        }else{
            $address = $query->get();
        }

        if (empty($address)){
            return self::response(self::CONTENT_IS_EMPTY);
        }
        return self::response(self::SUCCESS, $address);
    }

    public static function createAddress($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        $count = Address::where(function($query) use ($user_id){
            $query->where('user_id', $user_id);
        })->count();
        $address = new Address();
        $address->user_id = $user_id;
        $address->username = $data['username'];
        $address->postalcode = $data['postalcode'];
        $address->province = $data['province'];
        $address->city = $data['city'];
        $address->district = $data['district'];
        $address->detail_info = $data['detail_info'];
        $address->nationalcode = $data['nationalcode'];
        $address->mobile = $data['mobile'];
        // 如果是用户第一个添加的地址，设置为默认地址
        $address->is_default = $count ? 0 : 1;
        if (!$address->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS, $address->id);
    }

    public static function updateAddress($data)
    {
        $address = Address::find($data['address_id']);
        $address->username = $data['username'];
        $address->postalcode = $data['postalcode'];
        $address->province = $data['province'];
        $address->city = $data['city'];
        $address->district = $data['district'];
        $address->detail_info = $data['detail_info'];
        $address->nationalcode = $data['nationalcode'];
        $address->mobile = $data['mobile'];
        if (!$address->save()){
            return self::response(self::OBJECT_SAVE_FAIL);
        }
        return self::response(self::SUCCESS);
    }

    public static function deleteAddress($data)
    {
        $address = Address::find($data['address_id']);
        if (!$address->delete()){
            return self::response(self::OBJECT_DELETE_FAILD);
        }
        return self::response(self::SUCCESS);
    }

    public static function changeDefault($data)
    {
        $response = self::getUserIdByLoginSession($data['loginSession']);
        if ($response->code) {
            return self::response($response->code);
        }
        $user_id = $response->data;
        Address::where(function($query) use ($user_id, $data){
            $query->where('user_id', $user_id);
            $query->where('id', '!=', $data['address_id']);
        })->update(['is_default' => 0]);
        Address::where(function($query) use ($user_id, $data){
            $query->where('user_id', $user_id);
            $query->where('id', $data['address_id']);
        })->update(['is_default' => 1]);
        return self::response(self::SUCCESS);
    }
}